package com.yyjj.hangqing.kline;


import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.vinsonguo.klinelib.chart.TimeLineView;
import com.vinsonguo.klinelib.model.HisData;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.request.GetRequest;
import com.yyjj.hangqing.R;
import com.yyjj.hangqing.entity.BaseTResp;

import java.util.ArrayList;
import java.util.List;

import tech.com.commoncore.base.BaseFragment;
import tech.com.commoncore.utils.DateUtil;


public class TimeLineChartFragment extends BaseFragment{

    private static final String KEY_CODE = "code";

    private TimeLineView mTimelineview;
    private View layoutEmpty;
    private View layoutError;
    private View layoutLoading;
    private TextView tvRetry;
    private String path;
    private String code;
    private String type;
    public static TimeLineChartFragment newInstance(String code, String type) {
        TimeLineChartFragment fragment = new TimeLineChartFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_CODE, code);
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }
    public static TimeLineChartFragment newInstance(String code, String type , String path) {
        TimeLineChartFragment fragment = new TimeLineChartFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_CODE, code);
        bundle.putString("type", type);
        bundle.putString("path", path);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void beforeSetContentView() {
        super.beforeSetContentView();
        code = getArguments().getString(KEY_CODE);
    }

    @Override
    public int getContentLayout() {
        return R.layout.hq_fragment_time_line_chart1;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        layoutEmpty = mContentView.findViewById(R.id.layout_empty);
        layoutError = mContentView.findViewById(R.id.layout_error);
        layoutLoading = mContentView.findViewById(R.id.layout_loading);
        tvRetry = layoutError.findViewById(R.id.tv_retry);
        mTimelineview = mContentView.findViewById(R.id.timelineview);

        mTimelineview.setDateFormat(DateUtil.FORMAT_102);
        int count = 241;
        mTimelineview.setCount(count, count, count);
        type = getArguments().getString("type");
        path = getArguments().getString("path");
//        timeType = getArguments().getString("timeType");

        tvRetry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadData();
            }
        });

    }

    @Override
    public void loadData() {
        //K线数据
//    http://data.yingju8.com/yapi/Line/index?type=1&symbol=sh600552&line_type=3&is_domestic=2

        showTheLoading();
//        ViseHttp.GET(path==null ?"/yapi/Line/index":path)
//                .addParam("symbol", code)
//                .addParam("type", type);
        GetRequest request;
        if(path ==null){
            request = ViseHttp.GET("/yapi/Line/index")
//                .addParam("line_type", timeType)
//                .addParam("is_domestic", 2+"")
                    .addParam("symbol", code)
                    .addParam("type", type);
        }else{
//            http://data.fk7h.com/yapi/wankenet/get_line?name=wkb_bitcny&time=5
            request = ViseHttp.GET(path)
                    .addParam("name", code)
                    .addParam("time", type);
        }
//        request.request(new ACallback<BaseTResp<ArrayList<KLineEntity>>>() {
//                    @Override
//                    public void onSuccess(BaseTResp<ArrayList<KLineEntity>> data) {
//                        if (1 == data.code) {
//                            List<KLineEntity> kLineList = data.data;
//                            if (kLineList != null && kLineList.size() > 0) {
//                                List<HisData> hisData = new ArrayList<>();
//                                for (KLineEntity kLine : kLineList) {
//                                    if (kLine != null) {
//                                        HisData item = new HisData();
//                                        item.setClose(kLine.C);
//                                        item.setOpen(kLine.O);
//                                        item.setHigh(kLine.H);
//                                        item.setLow(kLine.L);
//                                        item.setVol(kLine.V);
//                                        item.setDate(kLine.Tick * 1000);
//                                        hisData.add(item);
//                                    }
//                                }
//                                mTimelineview.setLastClose(hisData.get(0).getClose());
//                                mTimelineview.initData(hisData);
//                                showContent();
//
//                            }
//                        } else {
//                            showEmpty();
//                        }
//                    }
//
//                    @Override
//                    public void onFail(int errCode, String errMsg) {
//                        showError();
//                    }
//                });
    }

    private void showTheLoading() {
        layoutEmpty.setVisibility(View.GONE);
        layoutError.setVisibility(View.GONE);
        layoutLoading.setVisibility(View.VISIBLE);
    }

    private void showEmpty() {
        layoutEmpty.setVisibility(View.VISIBLE);
        layoutError.setVisibility(View.GONE);
        layoutLoading.setVisibility(View.GONE);
    }

    private void showError() {
        layoutEmpty.setVisibility(View.GONE);
        layoutError.setVisibility(View.VISIBLE);
        layoutLoading.setVisibility(View.GONE);
    }

    private void showContent() {
        layoutEmpty.setVisibility(View.GONE);
        layoutError.setVisibility(View.GONE);
        layoutLoading.setVisibility(View.GONE);
    }



//    @Override
//    public boolean isRegisterEvent() {
//        return true;
//    }
//
//
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onRefreshKLineEvent(RefreshKLineEvent event) {
//        loadData();
//    }
}
